/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.ac.cam.ccn.sac.modules.privatemessage;

import uk.ac.cam.ccn.sac.modules.publicmessage.TextListener;
import java.io.IOException;
import java.sql.Timestamp;
import org.ccnx.ccn.CCNHandle;
import org.ccnx.ccn.config.ConfigurationException;
import org.ccnx.ccn.config.UserConfiguration;
import org.ccnx.ccn.impl.CCNFlowControl.SaveType;
import org.ccnx.ccn.io.content.CCNStringObject;
import org.ccnx.ccn.io.content.ContentEncodingException;
import org.ccnx.ccn.protocol.ContentName;
import org.ccnx.ccn.protocol.MalformedContentNameStringException;
import org.ccnx.ccn.protocol.PublisherPublicKeyDigest;
import uk.ac.cam.ccn.sac.modules.contacts.Contact;
import uk.ac.cam.ccn.sac.net.CCNNet;

/**
 *
 * @author Thomas
 */
public class PrivateTextListener extends TextListener{
    
    public PrivateTextListener(CCNNet net) throws ConfigurationException, IOException, MalformedContentNameStringException{
        super();
        this.net = net;
        
        // handle for wirtting text
        CCNHandle tempTextWriteHandle = CCNHandle.open();
        textNameSpace = ContentName.fromURI(net.getLocalContact().getAttribute(Contact.PRIVATE_MESSAGE));
        readString = new CCNStringObject(textNameSpace, (String)null, SaveType.RAW, textReadHandle);
        readString.updateInBackground(true);
        
        String introduction = UserConfiguration.userName() + " has entered " + textNameSpace;
        writeString = new CCNStringObject(textNameSpace, introduction, SaveType.RAW, tempTextWriteHandle);
        writeString.save();
    }
    
    @Override
    public void run(){
        Timestamp lastUpdate = null;
        do{
            try{
                synchronized(readString) {
                        readString.wait(500);
                }
            }catch(InterruptedException e){}
            try{
                if(readString.isSaved()){
                    Timestamp thisUpdate = readString.getVersion();
                    if(lastUpdate == null || thisUpdate.after(lastUpdate)){
                        lastUpdate=thisUpdate;
                        System.out.println("Got a text update "+thisUpdate);
                        
                        //lookup friendly name to display for this user.....
                        String userFriendlyName = getFriendlyName(readString.getContentPublisher());
                        if(!userFriendlyName.equals("")){
                            showMessage(userFriendlyName, thisUpdate, readString.string());
                        }
                    }
                }
            }catch(Exception e){}
        }while(isRunning);
    }
    
    private String getFriendlyName(PublisherPublicKeyDigest key){
        if(friendlyNames.containsKey(key))
            return friendlyNames.get(key).getAttribute(Contact.USER_NAME);
        return "";
    }
    
    private void showMessage(String name, Timestamp time, String message) throws ContentEncodingException, IOException {
        String msg = "["+name+" "+DATE_FORMAT.format(time)+"](private) "+message;
        net.showMessage(msg);        
    }
    
    public void sendText(String to, String msg) throws ContentEncodingException, IOException, MalformedContentNameStringException, ConfigurationException{
        Contact contact = nameToContact.get(to);
        if(contact!=null){
            if(contact.getAttribute(Contact.PRIVATE_MESSAGE)!=null){
                CCNHandle tempTextWriteHandle = CCNHandle.open();
                ContentName space = ContentName.fromURI(contact.getAttribute(Contact.PRIVATE_MESSAGE));
                CCNStringObject readPrivateString = new CCNStringObject(space, (String)null, SaveType.RAW, textReadHandle);
                readPrivateString.updateInBackground(true);

                CCNStringObject writePrivateString = new CCNStringObject(space, msg, SaveType.RAW, tempTextWriteHandle);
                writePrivateString.save();
            }
        }        
    }
}
